package com.study.common.util;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.json.JSONUtil;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据库加密解密工具类
 */
@Component
public class EncryptionUtil {
    /**
     *加密盐
     */
    public static final String SALT = "YONYOUZWJXRSJK2019";

    /**
     * 加密方法
     * @param content 加密的内容
     * @return 加密后的字符串
     */
    public static String encrypt(String content){
        StandardPBEStringEncryptor encryptor =new StandardPBEStringEncryptor();
        /*设置加密算法*/
        encryptor.setAlgorithm("PBEWithMD5AndDES");
        /*设置加密所需的salt(盐),提高加密性*/
        encryptor.setPassword(SALT);
        return encryptor.encrypt(content);
    }

    /**
     * 解密方法
     * @param content 解密的内容
     * @return 解密后的字符串
     */
    public static String decrypt (String content){
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        /*设置加密算法*/
        encryptor.setAlgorithm("PBEWithMD5AndDES");
        /*设置加密所需的salt(盐),提高加密性*/
        encryptor.setPassword(SALT);
        return encryptor.decrypt(content);
    }

    /**
     * 测试加密、解密
     * @param arg
     */
    public static void main(String[] arg){

        //获取到文件名
        String fileName = FileNameUtil.getName("阿斯顿发山东");
        String fileName1 = FileNameUtil.getName("http://10.56.0.180:80/SocialAssistance-be/sdLogin/loginByEncryption?encryption=8ed53e4246f16b236eb3449800c5f597&id=469ad596ab0a45b7ac24142a3cd0ac0a");
        System.out.println(fileName);
        System.out.println(fileName1);


        /*String pwd= EncryptionUtil.encrypt("iuapmz");
        System.out.println(pwd);
        String pwd2= EncryptionUtil.encrypt("2tpDr4tpk8Wgx6C3");
        System.out.println(pwd2);
        String pwd1=EncryptionUtil.decrypt("Z8QruhwxfzlpIBlTyaW0lQ==");
        System.out.println(pwd1);*/

        //第三方加密解密
        /*String id = "469ad596ab0a45b7ac24142a3cd0ac0a";
        String loginName = "jxzhyl";
        String p1 = loginName + id;
        String p2 = MD5Utils.MD5Encode(p1, "utf-8");
        System.out.println(p2);*/

        //第三方加密解密
        /*String id = "290e149386fa7af6e063ca00380a791d";
        String loginName = "jxbzpt";
        String p1 = loginName + id;
        String p2 = MD5Utils.MD5Encode(p1, "utf-8");
        System.out.println(p2);*/

        /*Map map = new HashMap();
        map.put("gmsfhm", "362422199810034312");
        String encrData = AESObjectUtil.jsonToAESString(JSONUtil.toJsonStr(map), null);
        System.out.println(encrData);*/
    }
}
